package findsqz;

import my.opencv.OpencvImageUtils;
import opencv.utils.InitUtils;
import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import static org.opencv.imgcodecs.Imgcodecs.imwrite;

public class MyFindContours {

    static{   InitUtils.addLibrary();
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME); }

    static String imgPath="C:\\Users\\admin\\Desktop\\cut\\0000.png";
    static String savePath="C:\\Users\\admin\\Desktop\\cut\\";

    public static void main(String[] args) {
        Mat src= Imgcodecs.imread(imgPath);
        Mat old = src.clone();
        //灰度化+二值化
        Mat gray=new Mat();
        Imgproc.cvtColor(src,gray, Imgproc.COLOR_BGR2GRAY);
        Imgproc.blur( gray, gray,new Size(1,1));
        imwrite(savePath+"zfx_gray.jpg",gray);
        Mat threshold=new Mat();
        Imgproc.threshold(gray,threshold,140,200, Imgproc.THRESH_BINARY);
        imwrite(savePath+"zfx_threshold.jpg",threshold);
        System.out.println("二值化通道数="+threshold.channels());

//        threshold = OpencvImageUtils.blur(threshold);
////        Imgcodecs.imwrite("C:/Users/admin/Desktop/cut/test_eye_blur.jpg", threshold);
//        threshold = OpencvImageUtils.dilate(threshold,4);
//        Imgcodecs.imwrite("C:/Users/admin/Desktop/cut/test_eye_dilate.jpg", threshold);
//        threshold = OpencvImageUtils.erode(threshold,1);
//        Imgcodecs.imwrite("C:/Users/admin/Desktop/cut/test_eye_erode.jpg", threshold);

        //轮廓查找
        List<MatOfPoint> contours=new ArrayList<MatOfPoint>();
        Mat hierarchy = new Mat();
        Imgproc.findContours(threshold,contours,hierarchy, Imgproc.RETR_CCOMP, Imgproc.CHAIN_APPROX_NONE);
        System.out.println("轮廓数量："+contours.size());
        System.out.println("轮廓类型："+hierarchy);

        Imgproc.drawContours(src,contours,-1,new Scalar(0,0,255),4);
        imwrite(savePath+"zfx_hierarchy.jpg",hierarchy);
        imwrite(savePath+"zfx_xxx.jpg",src);

        int point_x=0;
        int point_y=0;
        int point_width=0;
        int point_hight=0;


        Mat old2 = old.clone();
        for(int i=0;i<contours.size();i++){
            MatOfPoint point = contours.get(i);
            Rect rect= Imgproc.boundingRect(contours.get(i));
            System.out.println("  x:"+rect.x+"  y:"+rect.y+"  widyh:"+rect.width+"  height:"+rect.height);
//                List<MatOfPoint> contour=new ArrayList<MatOfPoint>();
//                contour.add(contours.get(i));
            Mat draw = OpencvImageUtils.drawRect(rect,old2);
            Imgcodecs.imwrite("C:/Users/admin/Desktop/cut/test_eye_draw"+i+".jpg", draw);
        }



        for(int k=0;k<hierarchy.cols();k++) {
            System.out.print("轮廓下标："+k +" { ");
            double[] ds = hierarchy.get(0, k);
            for (int l=0;l<ds.length;l++) {
                switch (l) {
                    case 0:
                        System.out.print(" 后一个轮廓下标："+ ds[l]);
                        break;
                    case 1:
                        System.out.print("  前一个轮廓下标："+ds[l]);
                        break;
                    case 2:
                        System.out.print("  子轮廓下标："+ds[l]);
                        break;
                    case 3:
                        System.out.print("  父轮廓下标："+ds[l]);
                        break;

                    default:
                        break;
                }
            }
            System.out.print(" }\n");
        }
    }

}
